package util.util.excel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import util.util.DevFileUtil;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author liW
 * @version 1.0
 * @date 2019-11-4 14:19
 */
public class WriteExcel {

    public static <T> void write(List<T> list, String outPath) {
        Workbook wb = model2Work(list, outPath);
        //生成
        DevFileUtil.createExcle(wb, outPath);
    }


    public static <T> void write(List<T> list, String outPath, String sheetName) {
        Workbook wb = model2Work(list, outPath, sheetName);
        //生成
        DevFileUtil.createExcle(wb, outPath);
    }

    /**
     * 生成Excel
     *
     * @param list
     */
    public static <T> Workbook model2Work(List<T> list, String outPath) {
        Workbook wb =  DevFileUtil.getWorkbook(outPath);
        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.createSheet();
        //表头
        Row row = sheet.createRow(0);
        //声明列对象
        Cell cell = null;
        T t = list.get(0);

        Field[] declaredFields = t.getClass().getDeclaredFields();
        //创建标题
        int n = 0;
        for (Field declaredField : declaredFields) {
            cell = row.createCell(n++);
            cell.setCellValue(declaredField.getName());
        }


        int rowCount = 0;

        for (T t1 : list) {
            row = sheet.createRow(++rowCount);
            n = 0;
            for (Field declaredField : declaredFields) {
                cell = row.createCell(n++);

                Object o = null;
                try {
                    declaredField.setAccessible(true);
                    o = declaredField.get(t1);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (o == null) {
                    cell.setCellValue("");
                } else {
                    cell.setCellValue(o.toString());
                }
            }

        }
        return wb;
    }




    /**
     * 生成Excel
     *
     * @param list
     */
    public static <T> Workbook model2Work(List<T> list, String outPath, String sheetName) {
        Workbook wb =  DevFileUtil.getWorkbook(outPath);
        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.createSheet(sheetName);
        //表头
        Row row = sheet.createRow(0);
        //声明列对象
        Cell cell = null;
        T t = list.get(0);

        Field[] declaredFields = t.getClass().getDeclaredFields();
        //创建标题
        int n = 0;
        for (Field declaredField : declaredFields) {
            cell = row.createCell(n++);
            cell.setCellValue(declaredField.getName());
        }


        int rowCount = 0;

        for (T t1 : list) {
            row = sheet.createRow(++rowCount);
            n = 0;
            for (Field declaredField : declaredFields) {
                cell = row.createCell(n++);

                Object o = null;
                try {
                    declaredField.setAccessible(true);
                    o = declaredField.get(t1);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (o == null) {
                    cell.setCellValue("");
                } else {
                    cell.setCellValue(o.toString());
                }
            }

        }
        return wb;
    }



    /**
     * 生成Excel
     *
     * @param map
     */
    public static <T> Workbook model2Work2(Map<String, List<T>> map, String outPath) {
        Workbook wb =  DevFileUtil.getWorkbook(outPath);

        Set<Map.Entry<String, List<T>>> entries = map.entrySet();
        T t = map.entrySet().stream().map(item -> item.getValue()).findFirst().get().get(0);
        Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Map.Entry<String, List<T>> entry : entries) {
            // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
            Sheet sheet = wb.createSheet(entry.getKey());
            //表头
            Row row = sheet.createRow(0);
            //声明列对象
            Cell cell = null;
            //创建标题
            int n = 0;
            for (Field declaredField : declaredFields) {
                cell = row.createCell(n++);
                cell.setCellValue(declaredField.getName());
            }
            int rowCount = 0;
            for (T t1 : entry.getValue()) {
                row = sheet.createRow(++rowCount);
                n = 0;
                for (Field declaredField : declaredFields) {
                    cell = row.createCell(n++);

                    Object o = null;
                    try {
                        declaredField.setAccessible(true);
                        o = declaredField.get(t1);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    if (o == null) {
                        cell.setCellValue("");
                    } else {
                        cell.setCellValue(o.toString());
                    }
                }

            }
        }


        return wb;
    }


    /**
     * 生成Excel
     *
     * @param list
     */
    public static Workbook map2Work(List<Map<String, String>> list, String outPath) {
        Workbook wb =  DevFileUtil.createWorkbook(outPath);

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        Sheet sheet = wb.createSheet();
        //表头
        Row row = sheet.createRow(0);
        //声明列对象
        Cell cell = null;
        Map<String, String> titleMap = list.get(0);
        Set<String> keys = titleMap.keySet();

        //创建标题
        int n = 0;
        for (String key : keys) {
            cell = row.createCell(n++);
            cell.setCellValue(key);
        }

        int rowCount = 0;
        for (Map<String, String> map : list) {
            row = sheet.createRow(++rowCount);
            n = 0;
            for (String key : keys) {
                cell = row.createCell(n++);
                cell.setCellValue(map.get(key));
            }
        }
        return wb;
    }


}
